package com.zy.scwuser.controller;

import com.zy.scwcommon.bean.AppResponse;
import com.zy.scwcommon.entity.TMember;
import com.zy.scwcommon.enums.AccttypeEnume;
import com.zy.scwcommon.enums.AuthEnume;
import com.zy.scwcommon.enums.UserTypeEnume;
import com.zy.scwcommon.vo.LoginRespVo;
import com.zy.scwuser.send.SendSms;
import com.zy.scwuser.service.TMemberService;
import com.zy.scwuser.vo.req.UserRegistVo;
import com.zy.scwuser.vo.resp.UserRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@Api(tags = "用户登陆注册模块")
@RequestMapping("/user")
@RestController
@Slf4j
public class UserLoginRegistController {

    @Autowired
    SendSms sendSms;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired(required = true)
    TMemberService tMemberService;
    @Value("${sms.phoneNum}")
    String phoneNum;  //手机号
    @Value("${sms.verifyCode}")
    String verifyCode; //验证码

    @ApiOperation(value = "用户登陆")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "登陆账号", name = "loginacct"),
            @ApiImplicitParam(value = "用户密码", name = "password")
    })
    @PostMapping("/login")
    public AppResponse<UserRespVo> login(@RequestParam("loginacct") String loginacct, @RequestParam("userpswd") String password) {
        try {
            UserRespVo userRespVo=tMemberService.getUserByLogin(loginacct,password);//业务层会生成一个accessToken存储到redis中会用到*******
            log.debug(loginacct);
            log.debug(password);
            return AppResponse.ok(userRespVo);
        } catch (Exception e) {
           e.printStackTrace();
           log.debug("登录失败---{}----{}",loginacct,e.getMessage());
            AppResponse resp = AppResponse.fail(null);
            resp.setMsg(e.getMessage());
            return resp;
        }
    }


    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2024/5/11
     * @Description: 应该需要传入参数的---只是暂时调用成功
     */
    @ApiOperation(value = "发送短信验证码")
    @PostMapping("/sendsms")
    public AppResponse<Object> sendsms() {
        AppResponse<String> stringAppResponse = sendSms.testSendSms();
        stringRedisTemplate.opsForValue().set(phoneNum, verifyCode);//redis不重启就会有，生产需要设置过期时间
        return AppResponse.ok(stringAppResponse);
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2024/5/11
     * @Description:
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public AppResponse<Object> register(UserRegistVo userRegistVo) {
        int i = tMemberService.saveTMember(userRegistVo);
        if (i > 0) {
            return AppResponse.ok("ok");
        }
        return AppResponse.fail("注册失败");
    }

    @ApiOperation(value = "验证短信验证码")
    @PostMapping("/valide")
    public AppResponse<Object> valide(String verifyCode) {
        String code = stringRedisTemplate.opsForValue().get(phoneNum);
        if(verifyCode==code){
            return AppResponse.ok("ok");
        }
        return AppResponse.fail("验证码错");
    }

    @ApiOperation(value = "重置密码")
    @PostMapping("/reset")
    public AppResponse<Object> reset(String loginacct,String password) {
        UserRespVo userByLogin = tMemberService.getUserByLogin(loginacct, password);
        return AppResponse.ok("ok");
    }


}
